// eslint-disable-next-line
import * as loginService from '@/api/user'
// eslint-disable-next-line
import { BasicLayout, BlankLayout, PageView, RouteView } from '@/layouts'

// 前端路由表
const constantRouterComponents = {
  // 基础页面 layout 必须引入
  BasicLayout: BasicLayout,
  BlankLayout: BlankLayout,
  RouteView: RouteView,
  PageView: PageView,
  // Dashboard
  // 'Adviser': () => import('@/views/adviser/Adviser'),
  // 'Data': () => import('@/views/adviser/Data'),
  'LeaderIndex': () => import('@/views/leader/LeaderIndex'),
  'CsoIndex': () => import('@/views/leader/CsoIndex'),
  'PerformanceIndex': () => import('@/views/performance/PerformanceIndex'),
  'Data': () => import('@/views/leader/Data'),
  // member
  'MemberList': () => import('@/views/member/MemberList'),
  'MemberSetting': () => import('@/views/member/MemberSetting'),
  'Setting': () => import('@/views/member/Setting'),
  'SubSetting': () => import('@/views/member/SubSetting'),
  'MemberDetail': () => import('@/views/member/MemberDetail'),
  'MemberKq': () => import('@/views/member/MemberKq'),
  'MemberCheck': () => import('@/views/member/MemberCheck'),
  'CheckDetail': () => import('@/views/member/CheckDetail'),
  'GroupManager': () => import('@/views/member/GroupManager'),
  'MemberBilled': () => import('@/views/member/MemberBilled'),
  'MemberView': () => import('@/views/member/MemberView'),
  'TimeoutCustomer': () => import('@/views/member/TimeoutCustomer'),
  'MemberReceive': () => import('@/views/member/MemberReceive'),
  // 微信
  'Wechat': () => import('@/views/wechat/Wechat'),
  'SubWechat': () => import('@/views/wechat/SubWechat'),
  'GoodWechat': () => import('@/views/wechat/GoodWechat'),
  'QuestionWechat': () => import('@/views/wechat/QuestionWechat'),
  'WechatState': () => import('@/views/wechat/WechatState'),
  'WechatSubState': () => import('@/views/wechat/WechatSubState'),
  'CheckWechat': () => import('@/views/wechat/CheckWechat'),
  // 客户
  'CustomerDetail': () => import('@/views/customer/CustomerDetail'),
  'CustomerList': () => import('@/views/customer/CustomerList'),
  'CustomerCheck': () => import('@/views/customer/CustomerCheck'),
  'CustomerReceive': () => import('@/views/customer/CustomerReceive'),
  'QstList': () => import('@/views/customer/QstList'),
  'ImportVip': () => import('@/views/customer/ImportVip'),
  'Console': () => import('@/views/console/Console'),
  'ApplicationSubject': () => import('@/views/customer/ApplicationSubject'),
  // 大客户
  'OtherCustomer': () => import('@/views/customer/OtherCustomer'),
  'OtherState': () => import('@/views/customer/OtherState'),
  'OtherDetail': () => import('@/views/customer/OtherDetail'),
  'OtherWechat': () => import('@/views/customer/OtherWechat'),
  'OtherPreWechat': () => import('@/views/customer/OtherPreWechat'),
  // 合同
  'ContractList': () => import('@/views/contract/ContractList'),
  'ContractDetail': () => import('@/views/contract/ContractDetail'),
  'RefundList': () => import('@/views/contract/RefundList'),
  // 'AddContract': () => import('@/views/contract/AddContract'),

  // 业务
  'BusinessList': () => import('@/views/business/BusinessList'),
  'BusinessDetail': () => import('@/views/business/BusinessDetail'),
  'HiddenBusiness': () => import('@/views/business/HiddenBusiness'),
  // 'StopList': () => import('@/views/business/StopList'),

  // 通话管理
  'AllCalls': () => import('@/views/calls/AllCalls'),
  'AllSubCalls': () => import('@/views/calls/AllSubCalls'),
  'GoodCall': () => import('@/views/calls/GoodCall'),
  'QuestionCall': () => import('@/views/calls/QuestionCall'),
  'CheckCall': () => import('@/views/calls/CheckCall'),
  'CallState': () => import('@/views/calls/CallState'),
  'CallSubState': () => import('@/views/calls/CallSubState'),

  // 个人日程
  'ScheduleSetting': () => import('@/views/schedule/ScheduleSetting'),
  'SchedulePlan': () => import('@/views/schedule/SchedulePlan'),
  'ScheduleUserList': () => import('@/views/schedule/UserList'),

  // 假期管理
  'HolidayManager': () => import('@/views/holiday/HolidayManager'),
  // bbs
  'BBSList': () => import('@/views/bbs/BBSList'),
  'BBSDetail': () => import('@/views/bbs/BBSDetail'),

  // 其他
  'ChangePassword': () => import('@/views/other/ChangePassword'),
  'Search': () => import('@/views/other/Search'),
  'BusinessPrice': () => import('@/views/other/BusinessPrice'),
  'ReceiveSetting': () => import('@/views/other/ReceiveSetting'),
  'IpConfig': () => import('@/views/other/IpConfig'),

  // 培训管理
  'Logs': () => import('@/views/video/Logs'),
  'Vestige': () => import('@/views/video/Vestige'),
  'VideoView': () => import('@/views/video/VideoView'),
  'VideoList': () => import('@/views/video/VideoList'),
  // 消息
  'AllMessage': () => import('@/views/message/AllMessage'),
  'MessageDetail': () => import('@/views/message/MessageDetail'),
  'MessageAdd': () => import('@/views/message/MessageAdd'),

  // 发票管理
  'CheckInvoice': () => import('@/views/invoice/CheckInvoice'),
  'InvoiceList': () => import('@/views/invoice/InvoiceList'),
  'InvoiceDetail': () => import('@/views/invoice/InvoiceDetail'),

  // 买卖管理
  'Intention': () => import('@/views/transaction/Intention'),
  'Purchase': () => import('@/views/transaction/Purchase'),
  'IntentDetail': () => import('@/views/transaction/components/IntentDetail'),
  'PurchaseDetail': () => import('@/views/transaction/components/PurchaseDetail'),
  'Wareroom': () => import('@/views/transaction/Wareroom'),
  'WareDetail': () => import('@/views/transaction/components/WareDetail')
}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*', redirect: '/404', hidden: true
}

// 根级菜单
const rootRouter = {
  path: '/',
  name: 'index',
  component: 'BasicLayout',
  redirect: '/leader',
  meta: { title: '系统首页' },
  children: []
}

/**
 * 动态生成菜单
 * @param token
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (token) => {
  return new Promise((resolve, reject) => {
    loginService.getNav(token).then(res => {
      const result = res.result.antd_menus
      const menuNav = []
      rootRouter.children = result
      menuNav.push(rootRouter)
      const routers = generator(menuNav)
      routers.push(notFoundRouter)
      resolve(routers)
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap) => {
  return routerMap.map(item => {
    const { title, show, hideChildren, hiddenHeaderContent, target, icon } = item.meta || {}
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.path || `${parent && parent.path || ''}/${item.key}`,
      // 路由名称，建议唯一
      name: item.name || item.key || '',
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.key],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: (constantRouterComponents[item.component || item.key]) || (() => import(`@/views/${item.component}`)),

      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: title,
        icon: icon || undefined,
        hiddenHeaderContent: hiddenHeaderContent,
        target: target
      }
    }
    // 是否设置了隐藏菜单
    if (show === false) {
      currentRouter.hidden = true
    }
    // 是否设置了隐藏子菜单
    if (hideChildren) {
      currentRouter.hideChildrenInMenu = true
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith('http')) {
      currentRouter.path = currentRouter.path.replace('//', '/')
    }
    // 重定向
    item.redirect && (currentRouter.redirect = item.redirect)
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      // Recursion
      currentRouter.children = generator(item.children, currentRouter)
    }
    return currentRouter
  })
}
